package algo.A00_前置基础;

public class M03_找出距离最近的两点 {
    public static void main(String[] args) {
        //二维数组保存每个点的坐标
        double[][] pointArray = {
                {1, 1},
                {1, 0.5},
                {2, 1},
                {3, 4},
                {2.2, 1},
                {1, 7},
                {4, 1.8},
                {1.1, 9},
                {3, 5},
                {5, 6}
        };
        double minDistance = Double.MAX_VALUE;
        int minI = 0, minJ = 0;
        for (int i = 0; i < pointArray.length - 1; i++) {
            for (int j = i + 1; j < pointArray.length; j++) {
                double x1 = pointArray[i][0];
                double y1 = pointArray[i][1];
                double x2 = pointArray[j][0];
                double y2 = pointArray[j][1];
                double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
                if (distance < minDistance) {
                    minDistance = distance;
                    minI = i;
                    minJ = j;
                }
            }
        }
        System.out.println("x1 = " + pointArray[minI][0] + " y1 = " + pointArray[minI][1]);
        System.out.println("x2 = " + pointArray[minJ][0] + " y2 = " + pointArray[minJ][1]);
        System.out.println("minDistance = " + minDistance);
    }
}
